package com.zsd.android.camerax.view.fragment

import android.graphics.Typeface
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.zsd.android.camerax.R
import com.zsd.android.camerax.adapter.HomeAdapter
import com.zsd.android.common.BaseFragment
import com.zsd.android.common.BaseModel
import com.zsd.android.common.BasePresenter
import com.zsd.android.common.IBaseView
import com.zsd.android.common.utils.MLog
import kotlinx.android.synthetic.main.fragment_home.view.*

/**
 * A simple [Fragment] subclass.
 */
class HomeFragment : BaseFragment<IBaseView, BaseModel<*>, BasePresenter<IBaseView, BaseModel<*>>>(),
    View.OnClickListener {

    private var homeRecycler: RecyclerView? = null
    val textStyles = intArrayOf(Typeface.NORMAL, Typeface.BOLD, Typeface.ITALIC, Typeface.BOLD_ITALIC)
    var index = 0

    companion object {

        @JvmStatic
        fun newInstance(): HomeFragment {
            return HomeFragment().apply {
                arguments = Bundle().apply {

                }
            }
        }
    }

    override fun layoutId(): Int {
        return R.layout.fragment_home
    }

    override fun initPresenter(): BasePresenter<IBaseView, BaseModel<*>>? {
        return null
    }

    override fun initView(savedInstanceState: Bundle?) {
        containerView?.findViewById<Button>(R.id.home_btn)?.setOnClickListener(this)
        homeRecycler = containerView?.findViewById(R.id.home_recycler)
        homeRecycler?.layoutManager = LinearLayoutManager(mContext)
        var list = ArrayList<String>()
        for (i in 0 .. 20) {
            list.add("item ---> ${i}")
        }
        var adapter = HomeAdapter(mContext!!, list)
        homeRecycler?.adapter = adapter
    }

    override fun onClick(view: View?) {
        when(view?.id) {
            R.id.home_btn -> {
                changeTextStyle(textStyles[index])
                index = (index + 1) % textStyles.size
            }
        }
    }

    private fun changeTextStyle(style: Int) {
        var adapter = (homeRecycler?.adapter as HomeAdapter)
        adapter.textStyles = style
        adapter.notifyDataSetChanged()
    }
}
